/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.milib.util {
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	/**
	 * @author Marek Brun
	 */
	public class MLBitmapUtil {
		
		public static function getBitmapDataByDisplay(display:DisplayObject, onbitmap:BitmapData=null):BitmapData {
			var bounds:Rectangle=display.getBounds(display);
			if(!onbitmap){
				onbitmap=new BitmapData(
					Math.max(1, Math.min(2880, bounds.width)),
					Math.max(1, Math.min(2880, bounds.height)),
					true,
					0x00000000
				);
			}
			onbitmap.draw(display, new Matrix(1, 0, 0, 1, -bounds.x, -bounds.y), null, null, null, true);
			
			return onbitmap;
		}
		
		public static function getBitmapByDisplay(display:DisplayObject, isRescale:Boolean=false):Bitmap {
			var bmp:Bitmap=new Bitmap(getBitmapDataByDisplay(display), "auto", true);
			if(isRescale){
				bmp.width=display.width;				bmp.height=display.height;
			}
			return bmp;
		}
		
		public static function getColorFromCenter(display:DisplayObject):Number {
			var bounds:Rectangle=display.getBounds(display);
			return getColorFromPoint(display, new Point(int(bounds.x+bounds.width/2), int(bounds.y+bounds.height/2)));
		}
		
		public static function getColorFromPoint(display:DisplayObject, point:Point):Number {
			var bd:BitmapData=new BitmapData(display.width, display.height);
			bd.draw(display);
			var color:Number=bd.getPixel(point.x, point.y);
			bd.dispose();
			return color;
		}
		
		public static function getBitmapDataByDisplayByParent(display:DisplayObject):BitmapData {
			var gotParent:Boolean=Boolean(display.parent);
			if(gotParent){
				var orgParent:DisplayObjectContainer=display.parent;
				var orgIndex:uint=display.parent.getChildIndex(display);
			}
			var sprite:Sprite=new Sprite();
			sprite.addChild(display);
			var bd:BitmapData=getBitmapDataByDisplay(sprite);
			sprite.removeChild(display);
			if(gotParent){
				orgParent.addChildAt(display, orgIndex);
			}
			return bd;
		}
		
		public static function mergeBitmapWithExactFit(bitmapPasteIn:BitmapData, bitmapToPaste:BitmapData, x:Number, y:Number, width:Number=NaN, height:Number=NaN):void {
			height= isNaN(height) ? bitmapToPaste.height : Math.round(height);
			width= isNaN(width) ? bitmapToPaste.width : Math.round(width);
			if(!width || !height){ return; }
			var matrix:Matrix=new Matrix();
			matrix.scale(width/bitmapToPaste.width, height/bitmapToPaste.height);
			var transformedBitmapToPaste:BitmapData=new BitmapData(width, height, true, 0x00000000);
			transformedBitmapToPaste.draw(bitmapToPaste, matrix, null, null, new Rectangle(0, 0, width, height), false);
			
			bitmapPasteIn.merge(transformedBitmapToPaste, new Rectangle(0, 0, width, height), new Point(Math.round(x), Math.round(y)), 0xFF, 0xFF, 0xFF, 0xFF);
			
			transformedBitmapToPaste.dispose();
		}
		
		public static function clear(bd:BitmapData):void {
			bd.fillRect(new Rectangle(0, 0, bd.width, bd.height), 0x00FFFFFF);
		}
	}
}
